﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Shsc.Net.Common.Validator.Validators;

namespace Shsc.Net.Common.Validator.Attributes
{
    /// <summary>
    /// 固定长度Check
    /// </summary>
    [AttributeUsage(AttributeTargets.Property)]
    public class FixLengthAttribute : BaseValidationAttribute
    {
        /// <summary>
        /// 
        /// </summary>
        private string ERROR_MESSAGE_ID = "HE_90007";

        /// <summary>
        /// 长度
        /// </summary>
        public int Length { get; set; }

        /// <summary>
        /// 是否按字节查看
        /// </summary>
        public bool IsByte { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="length"></param>
        public FixLengthAttribute(int length)
        {
            Length = length;
            IsByte = true;
            this.ErrorMessageId = ERROR_MESSAGE_ID;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="length"></param>
        public FixLengthAttribute(int length, bool isByte)
        {
            Length = length;
            IsByte = isByte;
            this.ErrorMessageId = ERROR_MESSAGE_ID;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected override BaseValidator CreateValidator()
        {
            if (ErrorMessageParam == null
                || ErrorMessageParam.Length == 0)
            {
                this.ErrorMessageParam = new String[] { Convert.ToString(Length) };
            }
            return new FixLengthValidator();
        }
    }
}
